package com.tcwlkj.uuService.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.adapter.AccountAdapter;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.MyAccountModel;
import com.tcwlkj.uuService.ui.set.BindBankCard;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.CommomDialog;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.MyRefresh;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/25 0025.
 */

public class MyAccount extends BaseActivity {
    @Bind(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    //    @Bind(R.id.yu_e_tv)
//    TextView yu_e_tv;
//    @Bind(R.id.ke_ti_xain_tv)
//    TextView ke_ti_xain_tv;
//    @Bind(R.id.today_sum_money)
//    TextView today_sum_money;
//    @Bind(R.id.lj_money)
//    TextView lj_money;
    @Bind(R.id.ti_xian)
    TextView ti_xian;

    private int pindex = 1;


    private AccountAdapter adapter;
    private MyAccountModel mAccountList = new MyAccountModel(new ArrayList<MyAccountModel.Rows>());
    private List<MyAccountModel.Rows> rowsList=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        ButterKnife.bind(this);
        initview();
    }

    private void initview() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getSystemService(Context.MEDIA_SESSION_SERVICE);
        }
        changeTitle("账户余额");
        MyRefresh.getInstance().getMusic(refreshLayout, false);
        adapter = new AccountAdapter(mAccountList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
//        yu_e_tv.setText(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance());
//        if(Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance())>200){
//            ke_ti_xain_tv.setText(Math.floor((Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance())-200)/100)*100 +"");
//        }else {
//            ke_ti_xain_tv.setText("0.00");
//        }
        AccountDetail(pindex);//获取交易流水
    }

    private void AccountDetail(int pindex) {
        Call<CommonResponse<MyAccountModel>> httpResultCall = RetrofitUtils.getRequest(this, false).flowrecord("trades", PreferencesUtils.getString(MyApp.getContext(), "userID"), "3", pindex, 10);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<MyAccountModel>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<MyAccountModel> commonResponse = (CommonResponse<MyAccountModel>) t;
                if (rowsList.size() >= Integer.parseInt(commonResponse.getTotal())) {
                    ToastUtil.getInstance().show("没有更多数据");
                    return;
                }
//                today_sum_money.setText(commonResponse.getData().getNowIncome());
//                lj_money.setText(commonResponse.getData().getTotalIncome());
                mAccountList=commonResponse.getData();
                rowsList.addAll(mAccountList.getRows());
                adapter.updateData(mAccountList,rowsList);
            }

            @Override
            public void onFail(int code, String message) {
                if (code == 40104) {
                    AccountDetail(pindex);
                } else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                rowsList.clear();
                pindex = 1;
                AccountDetail(pindex);
                refreshLayout.finishRefreshing();
            }
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pindex++;
                AccountDetail(pindex);
                refreshLayout.finishLoadmore();
            }
        });
        ti_xian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ChcheUtils.getInstance().getDriverDetailModel()==null){
                    ToastUtil.getInstance().show("账户已被注销");
                    return;
                }
                if (ChcheUtils.getInstance().getDriverDetailModel().getCard().getNumber() != null && !TextUtils.isEmpty(ChcheUtils.getInstance().getDriverDetailModel().getCard().getNumber())) {//有银行卡
                        Intent intent = new Intent(MyAccount.this, CashWithdrawalActivity.class);
                        if (Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance()) > 200) {
                            intent.putExtra("money", Math.floor((Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance()) - 200) / 100) * 100 + "");
                        } else {
                            intent.putExtra("money", "0.00");
                        }
                        startActivity(intent);
                } else {
                    new CommomDialog(baseContext, R.style.dialog, "暂无银行卡，是否添加?", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            startActivity(BindBankCard.class);
                        }
                    }).setTitle("提示").show();
                }
            }
        });
    }
    protected void onEventComing(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == 100) {//跑男详情刷新
//            yu_e_tv.setText(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance());
//            if(Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance())>200){
//                ke_ti_xain_tv.setText(Math.floor((Double.parseDouble(ChcheUtils.getInstance().getDriverDetailModel().getDetail().getDoBalance())-200)/100)*100 +"");
//            }else {
//                ke_ti_xain_tv.setText("0.00");
//            }
            rowsList.clear();
            pindex = 1;
            AccountDetail(pindex);
        }
    }

    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
